package com.xinchuang.domain.vo.byd;

import com.xinchuang.domain.type.NormalBYDQuotationaSubEnum;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 钢材成本
 */
@Data
public class SteelFeeVo {
    //小计
    private double subTotal;
    //数据
    private List<SteelFeeVo.DataList> dataList;

    @Data
    @AllArgsConstructor
    public class DataList {
        //项目
        private String itemName;
        //材料牌号/Specification
        private String specification;
        //尺寸-长/Size(mm×mm×mm)
        private double length;
        //尺寸-宽/Size(mm×mm×mm)
        private double width;
        // 尺寸-高/Size(mm×mm×mm)
        private double height;
        //数量
        private int count;
        //工时(小时)
        private Double hour;
        //重量/Weight(Kg)
        private double weight;
        //单价(元)
        private double price;
        //金额(元)
        private double totalPrice;
    }

    public void checkFee() {
        BigDecimal subTotalAfter = new BigDecimal(0.00);
        String strType = NormalBYDQuotationaSubEnum.STEEL.getCn();
        //检验小计
        for (DataList item : this.getDataList()) {
            double totalPrice1 = item.getTotalPrice();
            subTotalAfter = subTotalAfter.add(new BigDecimal(totalPrice1));
        }
        checkSubTotal(strType, this.getSubTotal(), subTotalAfter.setScale(2, RoundingMode.HALF_UP));
    }

    /**
     * 计算总价
     *
     * @param param1 单价
     * @param param2 工时、数量
     * @return
     */
    private BigDecimal calculateTotalPrice(Double param1, Double param2) {
        BigDecimal totalPrice = new BigDecimal(param1).multiply(new BigDecimal(param2)).setScale(2, RoundingMode.HALF_UP);
        return totalPrice;
    }


    /**
     * 验证小计是否一致
     *
     * @param type          类型
     * @param subTotal      前端入参
     * @param subTotalAfter 后端计算
     */
    private void checkSubTotal(String type, Double subTotal, BigDecimal subTotalAfter) {
        if (subTotalAfter.compareTo(BigDecimal.valueOf(subTotal)) != 0) {
            throw new RuntimeException(type + "-小计计算结果不一致。结果为" + subTotalAfter);
        }
    }
}
